package java_option.player.javacv.ext;

import java_option.player.javacv.core.AudioPlayThread;
import java_option.player.javafx.MainApplication;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.scene.control.Button;
import javafx.scene.control.Slider;
import javafx.scene.layout.StackPane;
import javafx.stage.WindowEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 声音大小调节
 * 
 * @author java红了 <br/>
 *         2020-9-2917:47:49
 */
public class AudioSoundUpdate {

	private static final Logger logger = LoggerFactory.getLogger(AudioSoundUpdate.class);

	private static AudioSoundUpdate instance;
	private final String tag = "AudioSoundUpdate";

	// 开关
	private Button mSoundSwitch;
	// 音量滑块
	private Slider mSoundSlider;
	// 当前声音大小
	private float soundVolume;

	private boolean isOpen = true;

	private AudioSoundUpdate() {

	}

	public static AudioSoundUpdate getInstance() {
		synchronized (AudioSoundUpdate.class) {
			if (instance == null) {
				instance = new AudioSoundUpdate();
			}
		}
		return instance;
	}

	public void init(Button soundSwitch, Slider slider) {
		this.mSoundSwitch = soundSwitch;
		this.mSoundSlider = slider;
		// 设置开关之前先记录一下当前的音量
		this.soundVolume = AudioPlayThread.getInstance().getSoundVolume();
		// 打开 或者关闭声音
		mSoundSwitch.setOnMouseClicked(new EventHandler<Event>() {
			@Override
			public void handle(Event event) {
				isOpen = !isOpen;
				// 关闭声音
				if (isOpen == false) {
					mSoundSlider.setVisible(false);
					mSoundSwitch.setText("声音(关)");
					AudioPlayThread.getInstance().setSoundVolume(0);
				} else {
					mSoundSlider.setVisible(true);
					mSoundSwitch.setText("声音(开)");
					AudioPlayThread.getInstance().setSoundVolume(soundVolume);
				}
			}
		});

		// 2、初始化声音大小滑条
		mSoundSlider.setMin(1.0);
		mSoundSlider.setMax(100.0);
		MainApplication.primaryStage.addEventHandler(WindowEvent.WINDOW_SHOWN, new EventHandler<WindowEvent>() {
			@Override
			public void handle(WindowEvent window) {
				StackPane trackPane = (StackPane) mSoundSlider.lookup(".track");
				// 范围 [0.01,1]
				float defaultVolume = AudioPlayThread.getInstance().getSoundVolume();
				String style = String.format("-fx-background-color: linear-gradient(to right, #2D819D %f, #969696 %f);", defaultVolume, defaultVolume);
				trackPane.setStyle(style);
				mSoundSlider.setValue(defaultVolume * 100);
				mSoundSlider.valueProperty().addListener(new ChangeListener<Number>() {
					@Override
					public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
						int newInt = newValue.intValue();
						logger.debug( "改变音量 " + newInt / 100.0f);
						// 设置音量
						float volume = newInt / 100.0f;
						AudioPlayThread.getInstance().setSoundVolume(volume);
						// 记录当前音量
						soundVolume = volume;
						// 改变 slider样式
						String style = String.format("-fx-background-color: linear-gradient(to right, #2D819D %d%%, #969696 %d%%);", newInt, newInt);
						trackPane.setStyle(style);
					}
				});
			}
		});

	}
}
